/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package util.ejb;

import entities.tools.Form;
import entities.users.AdminUser;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

/**
 * Bean used for performing the operations on the Form Owners entities
 *
 * @author Marek
 */
@Stateless
public class AdminUserBean implements AdminUserBeanInterface {

    @PersistenceContext(name = "test", unitName = "JEE-study-warPU")
    private EntityManager em;

    /**
     * Method creates new AdminUser instance
     *
     * @return new AdminUser instance
     */
    @Override
    public AdminUser createAdminUser() {
        AdminUser adminUser = new AdminUser();
        em.persist(adminUser);
        return adminUser;
    }

    /**
     * Method deletes specified by detachedFormOnwer entity
     *
     * @param detachedAdminUser
     * @return true if operation succeeded, false - if not
     */
    @Override
    public boolean deleteAdminUser(AdminUser detachedAdminUser) {
        AdminUser attachedAdminUser = em.find(AdminUser.class, detachedAdminUser.getId());
        if (null == attachedAdminUser) {
            System.out.println("AdminUserBean:deleteAdminUser():AdminUser, you have specified, doesn't exist in the database!");
            return false;
        }
        em.remove(attachedAdminUser);
        return true;
    }

    //updates the existing form owner
    /**
     * Method updates in the database, specified by detachedAdminUser entity
     *
     * @param detachedAdminUser
     * @return true if operation succeeded, false - if not
     */
    @Override
    public boolean updateAdminUser(AdminUser detachedAdminUser) {
        AdminUser attachedAdminUser = em.find(AdminUser.class, detachedAdminUser.getId());
        if (null == attachedAdminUser) {
            System.out.println("AdminUserBean:updateAdminUser():AdminUser, you have specified, doesn't exist in the database!");
            return false;
        }
        em.merge(detachedAdminUser);
        return true;
    }

    /**
     * Method find in the detabase entity specified by id parameter, and returns
     * this entity
     *
     * @param id
     * @return founded AdminUser
     */
    @Override
    public AdminUser getAdminUser(Long id) {
        AdminUser attachedAdminUser = em.find(AdminUser.class, id);
        if (null == attachedAdminUser) {
            System.out.println("AdminUserBean:getAdminUser():AdminUser, you have specified, doesn't exist in the database!");
            return null;
        }
        return attachedAdminUser;
    }

    /**
     * Method returns all AdminUser entities from the database
     *
     * @return all AdminUser from the database
     */
    @Override
    public List<AdminUser> getAdminUsers() {
        return em.createQuery("select adminUser from AdminUser adminUser order by adminUser.id asc", AdminUser.class).getResultList();
    }

    
    /**
     * Method returns all AdminUser entities from the database
     *
     * @param orderBy
     * @return all AdminUser from the database
     */
    @Override
    public List<AdminUser> getAdminUsers(String orderBy,String kierunek) {
        return em.createQuery("select adminUser from AdminUser adminUser order by adminUser."+orderBy+" "+kierunek, AdminUser.class).getResultList();
    }

    @Override
    public List<AdminUser> getAdminUsers(String orderBy,String kierunek,AdminUser adminUser) {
        return em.createQuery("select adminUser from AdminUser adminUser where adminUser.login like :login and adminUser.password like :password and adminUser.firstName like :firstName and adminUser.secondName like :secondName and adminUser.email like :email and adminUser.phone like :phone and adminUser.userGroup like :userGroup order by adminUser."+orderBy+" "+kierunek, AdminUser.class)
                .setParameter("login","%"+adminUser.getLogin()+"%")
                .setParameter("password","%"+adminUser.getPassword()+"%")
                .setParameter("firstName","%"+adminUser.getFirstName()+"%")
                .setParameter("secondName","%"+adminUser.getSecondName()+"%")
                .setParameter("phone","%"+adminUser.getPhone()+"%")
                .setParameter("email","%"+adminUser.getEmail()+"%")
                .setParameter("userGroup","%"+adminUser.getUserGroup()+"%")
                .getResultList();
    }
    /**
     * (Dangerous) Method is bypasses security policy. Normally AdminUser cannot
     * be deleted, when he had filled some forms before. Method is performing
     * delete operation on all instances of FilledForm entity related to the
     * Form Owner, specified by pram, and than it deletes also that AdminUser
     * entity, specified by param.
     *
     * @param detachedAdminUser
     * @return true - if operation succeeded false - otherwise
     */
    //TODO: Method need to be tested, if it works correctly
    @Override
    public boolean forcedDeleteAdminUser(AdminUser detachedAdminUser) {
        /*
        //TODO: create that method in the future
        */
        return true;
    }
    
    
    //TODO: register the existing form, as a property of the existing form owner
    //TODO: remove the existing form, from the existing form owner, property list
    //TODO: register the existing form admin, as a connected to the form owner
    //TODO: remove the existing form admin, from the form owner connections with form admins list
}
